package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.marktotop.entity.Admin;
import com.marktotop.entity.Employee;
import com.marktotop.entity.LoginLog;
import com.marktotop.mapper.LoginLogMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ILoginLogService;
import com.marktotop.utils.AdminUtils;
import com.marktotop.utils.paua.AddressUtil;
import com.marktotop.utils.paua.IPUtil;
import com.marktotop.vo.AdminVO;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.omg.CORBA.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * LoginLogServiceImpl
 *
 * @author WXLD
 * @Data 2021/7/1
 * @Since 1.0
 */
@Service
public class LoginLogServiceImpl implements ILoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 插入登入日志
     * @param request
     */
    @Override
    public void add(HttpServletRequest request) {
        loginLogMapper.insert(createLoginLog(request));
    }


    /**
     * 创建登入日志
     * @param
     * @return
     */
    public static LoginLog createLoginLog(HttpServletRequest request) {
        // 获得当前登录用户
        Admin currentAdmin = AdminUtils.getCurrentAdmin();
        // 创建登录日志对象
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(currentAdmin.getUsername());
        loginLog.setIp(IPUtil.getIpAddr(request));
        loginLog.setLocation(AddressUtil.getCityInfo(IPUtil.getIpAddr(request)));
        // 获取客户端操作系统
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        OperatingSystem os = userAgent.getOperatingSystem();
        loginLog.setUserSystem(os.getName());
        loginLog.setUserBrowser(browser.getName());
        loginLog.setLoginTime(new Date());
        return loginLog;
    }



    /**
     * 删除登录日志
     *
     * @param id
     * @throws SystemException
     */
    @Transactional
    @Override
    public ResultBean deleteLog(Long id) throws SystemException {
        LoginLog loginLog = loginLogMapper.selectById(id);
        if(null == loginLog){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        loginLogMapper.deleteById(id);
        return ResultBean.ok().message("登录日志删除成功！");
    }

    /**
     * 批量删除登入日志
     *
     * @param asList
     * @return
     */
    @Override
    public Boolean batchDeleteLogs(List<Long> asList) {
        List<LoginLog> loginLogs = loginLogMapper.selectBatchIds(asList);
        if(null == loginLogs){
            return false;
        }
        if (1 == loginLogMapper.deleteBatchIds(asList)){
            return true;
        }
        return false;
    }


    /**
     * 分页展示登录日志列表
     *
     * @param currentPage
     * @param size
     * @param loginLog
     * @return
     */
    @Override
    public ResultBean findLoginLogList(Integer currentPage, Integer size, LoginLog loginLog) {
        // 开启分页
        Page<LoginLog> page = new Page<>(currentPage,size);
        Page<LoginLog> loginLogPage = loginLogMapper.selectPage(page, null);
        if (null == loginLogPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有登录日志信息成功！")
                .data("total",loginLogPage.getTotal())
                .data("loginLogs",loginLogPage.getRecords());
    }

    /**
     * 登入报表
     * @param adminVO
     * @return
     */
    @Override
    public List<Map<String, Object>> loginReport(AdminVO adminVO) {
        return loginLogMapper.userLoginReport(adminVO);
    }
}
